using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour {
	
	
	public int moveSpeed = 250;
	public bool isComputer = false;
	Vector3 computerDirection = Vector3.left;
	public Transform BombObj;
	public int time = 0;
	public static int lives = 3;
	public static int score = 0;
	// Use this for initialization
	void Start () 
	{
		lives =3;

	}

	// Update is called once per frame
	void Update () 
	{
		if(!isComputer) PlayerMove();
		else 
		{
			ComputerMove();
			DropBomb();
			timeKeep();
		}
		if(lives == 0)
			Application.LoadLevel(3);
	}
	
	void timeKeep()
	{
		time++;
	}
	void DropBomb()
		{
		
			if(time >= 100/MainMenu.difficulty)
			{
				Vector3 newPosition2 = transform.position;
				newPosition2.y += -30;
				Transform newBomb = (Transform)Instantiate(BombObj,newPosition2, Quaternion.identity);	
				time = 0;
			}
	}
	void ComputerMove()
	{
		Vector3 newPosition = Vector3.zero;
		newPosition = computerDirection * (moveSpeed * Time.deltaTime);
		
		newPosition = newPosition + transform.position;
		
		transform.position = newPosition;
		
		if(newPosition.x > 600)
		{
			newPosition.x = 600;
			computerDirection.x *= -1;
		}
		else if(newPosition.x < -600)
		{
			newPosition.x = -600;
			computerDirection.x *= -1;
		}
	}
	
	void PlayerMove()
	{
		Vector3 moveDirection = Vector3.zero;
		Vector3 newPosition = Vector3.zero;
		
		if(Input.GetKey(KeyCode.LeftArrow)) moveDirection.x -= 1;
		if(Input.GetKey(KeyCode.RightArrow)) moveDirection.x += 1;
		
		moveDirection = moveDirection * (moveSpeed * Time.deltaTime);
		
		newPosition = transform.position + moveDirection;
		
		newPosition.x = Mathf.Clamp(newPosition.x, -600, 600);
		
		transform.position = newPosition;
	}
}